<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getLineChart } from '@/api/dashboard/smsk'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      setTimeoutTimer: null,
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.setTimeoutTimer) {
        clearTimeout(this.setTimeoutTimer)
      }
      this.initChart()
    })
  },
  beforeDestroy() {
    if (this.setTimeoutTimer) {
      clearTimeout(this.setTimeoutTimer)
    }
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      getLineChart().then(response => {
        this.chart = echarts.init(this.$el, 'macarons')
        const _this = this
        this.chart.on('click', function(params) {
          if (params && params.name) {
            _this.$emit('checkDate', params.name)
          }
        })
        this.setOptions(response.data)
        clearTimeout(this.setTimeoutTimer)
        this.setTimeoutTimer = setTimeout(this.initChart, 60 * 1000)
      })
    },
    setOptions({ inputAmountData, checkAmountData, counterfeitAmountData, totalBagsData, checkedBagsData, inputBagsData, dateRange } = {}) {
      this.chart.setOption({
        title: {
          text: '缴款单数据趋势',
          x: 'center'
        },
        xAxis: {
          data: dateRange,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [{
          name: '金额',
          type: 'value',
          axisTick: {
            show: false
          }
        }, {
          name: '缴款单数',
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        legend: {
          top: '25px',
          data: ['缴款单金额', '清点金额', '入账金额', '缴款单笔数', '清点笔数', '入账笔数']
        },
        series: [{
          name: '缴款单金额', itemStyle: {
            normal: {
              color: '#0000FF',
              lineStyle: {
                color: '#0000FF',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: inputAmountData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut',
          yAxisIndex: 0
        },
        {
          name: '清点金额',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#6600FF',
              lineStyle: {
                color: '#6600FF',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: checkAmountData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          yAxisIndex: 0
        },
        {
          name: '入账金额',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: 'green',
              lineStyle: {
                color: 'green',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: counterfeitAmountData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          yAxisIndex: 0
        },
        {
          name: '缴款单笔数',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#A00066',
              lineStyle: {
                color: '#A00066',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: totalBagsData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          yAxisIndex: 1
        },
        {
          name: '清点笔数',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#CC0033',
              lineStyle: {
                color: '#CC0033',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: checkedBagsData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          yAxisIndex: 1
        },
        {
          name: '入账笔数',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: 'purple',
              lineStyle: {
                color: 'purple',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: inputBagsData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          yAxisIndex: 1
        }]
      })
    }
  }
}
</script>
